package com.ardf.controller;

import com.ardf.entity.Competition;
import com.ardf.service.ICompetitionService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@RestController
@RequestMapping("/ardf/competition")
public class CompetitionController {

    private final ICompetitionService competitionService;

    public CompetitionController(ICompetitionService competitionService) {
        this.competitionService = competitionService;
    }

    /**
     * 分页
     *
     * @param pageSize 每页多少条
     * @param pageNum  第几页
     * @return 分页数据
     * @author ShiYunHao
     * @since 2025-01-10
     */
    @GetMapping("/page")
    public ResponseEntity<Page<Competition>> page(@RequestParam("pageSize") Integer pageSize,
                                                  @RequestParam("pageNum") Integer pageNum) {
        System.out.println("saassasa");
        Page<Competition> page = competitionService.page(new Page<>(pageNum, pageSize));
        return ResponseEntity.ok(page);
    }

    /**
     * 新增赛事
     *
     * @param competition
     * @author ShiYunHao
     * @since 2025-01-12
     */
    @PostMapping("/add")
    public void add(@RequestBody Competition competition) {
        competitionService.save(competition);
    }

    /**
     * 更新进度条
     */
    @PostMapping("/updateProgress")
    public void updateProgress(@RequestParam(value = "competitionId") Long id, @RequestParam("progress") Byte progress) {
        competitionService.update(new LambdaUpdateWrapper<Competition>()
                .set(Competition::getProgress, progress).eq(Competition::getId, id));
    }
    /**
     * 更新赛事
     */
    @PostMapping("/update")
    public void update(@RequestBody Competition competition){
        competitionService.updateById(competition);
    }

    /**
     * 批量删除
     */
    @PostMapping("/deleteBatch")
    public void deleteBatch(@RequestBody List<Long> ids){
        competitionService.deleteAll(ids);
    }
}
